<template>
  <div class="container">
    <div class="page-header">
      <div class="header-content">
        <div class="back-button">
          <el-button
            @click="$router.push({ name: 'Jxjiangjjs' })"
            icon="el-icon-back"
            >返回</el-button
          >
        </div>
        <h1>绩效奖金基数报批表列表</h1>
      </div>
    </div>

    <div class="main-content">
      <div class="content-wrapper">
        <div class="search-bar">
          <el-form :inline="true" :model="searchForm">
            <el-form-item label="工号">
              <el-input
                v-model="searchForm.employeeId"
                placeholder="请输入工号"
              ></el-input>
            </el-form-item>
            <el-form-item label="姓名">
              <el-input
                v-model="searchForm.name"
                placeholder="请输入姓名"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                @click="handleSearch"
                icon="el-icon-search"
                >查询</el-button
              >
              <el-button @click="resetSearch" icon="el-icon-refresh"
                >重置</el-button
              >
              <el-button type="primary" @click="handleAdd" icon="el-icon-plus"
                >新增</el-button
              >
            </el-form-item>
          </el-form>
        </div>

        <div style="padding: 20px; background-color: #fff">
          <el-table
            :data="tableData"
            border
            style="width: 100%"
            v-loading="loading"
          >
            <el-table-column
              prop="employeeId"
              label="工号"
              width="120"
            ></el-table-column>
            <el-table-column
              prop="name"
              label="姓名"
              width="120"
            ></el-table-column>
            <el-table-column
              prop="department"
              label="任职部门"
              width="180"
            ></el-table-column>
            <el-table-column
              prop="position"
              label="职务"
              width="120"
            ></el-table-column>
            <el-table-column
              prop="applyBonusBase"
              label="申请绩效奖金基数"
              width="150"
            ></el-table-column>
            <el-table-column
              prop="effectiveDate"
              label="生效日期"
              width="120"
            ></el-table-column>
            <el-table-column prop="status" label="状态" width="100">
              <template slot-scope="scope">
                <el-tag :type="getStatusType(scope.row.status)">{{
                  getStatusText(scope.row.status)
                }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="300" fixed="right">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="info"
                  @click="handleView(scope.row)"
                  icon="el-icon-view"
                  >查看</el-button
                >
                <el-button
                  size="mini"
                  type="primary"
                  @click="handleEdit(scope.row)"
                  icon="el-icon-edit"
                  >编辑</el-button
                >
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row)"
                  icon="el-icon-delete"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>

        <div class="pagination">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.current"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getPage, removeById } from "@/api/performance-bonus-base";

export default {
  data() {
    return {
      loading: false,
      searchForm: {
        employeeId: "",
        name: "",
      },
      page: {
        current: 1,
        size: 10,
        total: 0,
      },
      tableData: [],
    };
  },
  created() {
    this.loadData();
  },
  methods: {
    async loadData() {
      this.loading = true;
      try {
        const { data } = await getPage({
          current: this.page.current,
          size: this.page.size,
          employeeId: this.searchForm.employeeId,
          name: this.searchForm.name,
        });
        this.tableData = data.data.records;
        this.page.total = data.data.total;
      } catch (error) {
        this.$message.error("加载数据失败");
      } finally {
        this.loading = false;
      }
    },
    handleSearch() {
      this.page.current = 1;
      this.loadData();
    },
    resetSearch() {
      this.searchForm = {
        employeeId: "",
        name: "",
      };
      this.handleSearch();
    },
    handleSizeChange(val) {
      this.page.size = val;
      this.loadData();
    },
    handleCurrentChange(val) {
      this.page.current = val;
      this.loadData();
    },
    handleAdd() {
      this.$router.push({
        name: "Jxjiangjjs",
        query: { type: "add" },
      });
    },
    handleView(row) {
      this.$router.push({
        name: "Jxjiangjjs",
        query: { id: row.id, type: "view" },
      });
    },
    handleEdit(row) {
      this.$router.push({
        name: "Jxjiangjjs",
        query: { id: row.id, type: "edit" },
      });
    },
    async handleDelete(row) {
      try {
        await this.$confirm("确认删除该记录?", "提示", {
          type: "warning",
        });
        await removeById(row.id);
        this.$message.success("删除成功");
        this.loadData();
      } catch (error) {
        if (error !== "cancel") {
          this.$message.error("删除失败");
        }
      }
    },
    getStatusType(status) {
      const types = {
        0: "info",
        1: "warning",
        2: "success",
        3: "danger",
      };
      return types[status] || "info";
    },
    getStatusText(status) {
      const texts = {
        0: "草稿",
        1: "审核中",
        2: "已通过",
        3: "已驳回",
      };
      return texts[status] || "未知";
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
  min-height: calc(100vh - 40px);

  .page-header {
    padding: 16px 20px;
    background-color: #fff;
    border-bottom: 1px solid #dcdfe6;
    display: flex;
    justify-content: center;
    width: 1160px;
    max-width: 100%;
    margin: 0 auto;
    border-radius: 5px;

    .header-content {
      width: 1160px;
      max-width: 100%;
      display: flex;
      align-items: center;

      .back-button {
        margin-right: 20px;
      }

      h1 {
        margin: 0;
        font-size: 20px;
        font-weight: 500;
        color: #303133;
      }
    }
  }
  .el-form-item {
    margin-bottom: 0px;
  }
  .main-content {
    flex: 1;
    padding: 20px;
    // background-color: #f5f7fa;
    overflow: auto;
    display: flex;
    justify-content: center;

    .content-wrapper {
      width: 1200px;
      max-width: 100%;
    }
  }
}

.search-bar {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.el-table {
  margin-top: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination {
  margin-top: 20px;
  text-align: right;
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
</style> 